package com.meizhuo.listview;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageView;

import com.bumptech.glide.Glide;
import com.meizhuo.listview.DataSaveUtil.ListDataSave;
import com.meizhuo.listview.network_data.send_data.Register;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@SuppressLint("ValidFragment")
public class MyFragment3 extends Fragment implements View.OnClickListener {

    int[] arr = new int[]{
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3
    };

    String[][] str = new String[][]{
            {"支付"},
            {"收藏"},
            {"相册"}
    };

    HashMap<String, Object> hashMap = new HashMap<>();

    private Context context;
    private AppCompatImageView me_pic;
    private TextView me_nickname;
    private TextView me_username;
    private ListView list_view;
    private AppCompatButton loginout;

    public MyFragment3(Context context) {
        this.context = context;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.melayout, container, false);

        // 具体的实现方法：
        List<HashMap<String, Object>> listData = new ArrayList();

        for (int i = 0; i < str.length; i++) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("pic", arr[i]);
            hashMap.put("title", str[i][0]);
            listData.add(hashMap);
        }

        // 将 hashMap 的 key 组成一个字符串数组
        String[] form = new String[]{"pic", "title"};
        // 将 item 布局中的 view 的 id 组成一个数组，要和 form 对应
        int[] to = new int[]{R.id.pic, R.id.title};
        SimpleAdapter simpleAdapter = new SimpleAdapter(context, listData, R.layout.me, form, to);
        ListView listView = view.findViewById(R.id.list_view);
        listView.setAdapter(simpleAdapter);
        initView(view);

        Register user = ListDataSave.getInstance(context,"Userdata").getData("user");
        System.out.println(user);
        me_nickname.setText("昵称："+user.getNickname());
        me_username.setText("账号："+user.getUsername());
        Glide.with(me_pic.getContext()).load(user.getAvatar()).into(me_pic);


        return view;
    }

    private void initView(View view) {
        me_pic = (AppCompatImageView) view.findViewById(R.id.me_pic);
        me_nickname = (TextView) view.findViewById(R.id.me_nickname);
        me_username = (TextView) view.findViewById(R.id.me_username);
        list_view = (ListView) view.findViewById(R.id.list_view);
        loginout = (AppCompatButton) view.findViewById(R.id.loginout);
        loginout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginout:

                ListDataSave.getInstance(context,"Userdata").removeData("user");
                // 进行跳转
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
                break;
        }
    }
}
